import request from '@/utils/request';
import { download, toFormData, checkDownloadRes } from '@/utils/common';

/**
 * 分页查询化工材料盘点
 */
export async function pageMesChemicalInventoryChecks(params) {
  const res = await request.get('/mes/mesChemicalInventoryCheck/list', { params });
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 查询全部化工材料盘点
 */
export async function listMesChemicalInventoryChecks(params) {
  const res = await request.get('/mes/mesChemicalInventoryCheck/list', { params });
  if (res.data.code === 200) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 根据id查询化工材料盘点
 */
export async function getMesChemicalInventoryCheck(id) {
  const res = await request.get('/mes/mesChemicalInventoryCheck/' + id);
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加化工材料盘点
 */
export async function addMesChemicalInventoryCheck(data) {
  const res = await request.post('/mes/mesChemicalInventoryCheck', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改化工材料盘点
 */
export async function updateMesChemicalInventoryCheck(data) {
  const res = await request.put('/mes/mesChemicalInventoryCheck', data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除化工材料盘点
 */
export async function removeMesChemicalInventoryCheck(id) {
  const res = await request.delete('/mes/mesChemicalInventoryCheck/' + id);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除化工材料盘点
 */
export async function removeMesChemicalInventoryChecks(ids) {
  const res = await request.delete('/mes/mesChemicalInventoryCheck/' + ids.join());
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 导出化工材料盘点
 */
export async function exportMesChemicalInventoryChecks(params) {
  const res = await request({
    url: '/mes/mesChemicalInventoryCheck/export',
    method: 'POST',
    data: toFormData(params),
    responseType: 'blob'
  });
  await checkDownloadRes(res);
  download(res.data, `mesChemicalInventoryCheck_${Date.now()}.xlsx`);
}
